package com.tange.ai.iot.client.gnssa.utils;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.ArrayList;
import java.util.List;

public class JsonConverter {
    private static final ObjectMapper mapper = new ObjectMapper()
            .enable(SerializationFeature.INDENT_OUTPUT);

    public static String toJson(VrThumbnailParser.HyVrThumbnailResp resp)
            throws JsonProcessingException {

        // 创建可序列化的DTO对象
        JsonResponse jsonResp = new JsonResponse(
                resp.over,
                resp.num,
                new ArrayList<>()
        );

        for (VrThumbnailParser.VrThumbnailJpeg jpeg : resp.jpegs) {
            jsonResp.thumbnails.add(new JsonThumbnail(
                    jpeg.size,
                    jpeg.timestamp,
                    jpeg.getDataBase64()  // 使用Base64编码
            ));
        }

        return mapper.writeValueAsString(jsonResp);
    }

    // JSON数据结构
    public static class JsonResponse {
        public boolean over;
        public int num;
        public List<JsonThumbnail> thumbnails = new ArrayList<>();

        public JsonResponse(boolean over, int num, List<JsonThumbnail> thumbnails) {
            this.over = over;
            this.num = num;
            this.thumbnails = thumbnails;
        }
    }

    public static class JsonThumbnail {
        public int size;
        public int timestamp;
        public String data;  // Base64字符串

        public JsonThumbnail(int size, int timestamp, String data) {
            this.size = size;
            this.timestamp = timestamp;
            this.data = data;
        }
    }
}
